package controller;

import entity.Address;
import entity.Orders;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import relationEntity.OrderAddress;
import service.OrderService;
import util.Page;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by jifeng on 2016/12/15 0015.
 */
@Controller
public class OrderController {
    @Resource(name="orderService")
    private OrderService orderService;

    /**
     * Created by jifeng
     * 查询订单列表
     * */
    @RequestMapping(value = "/orderList")
    public Model orderList(HttpServletRequest request, HttpServletResponse response,Model mv){
        try {
            String pageNo =request.getParameter("pageNo");
            if (pageNo == null){
                pageNo = "1";
            }
            String userId = request.getParameter("userId");
            String paymentStatus = request.getParameter("paymentStatus");
            String sendStatus = request.getParameter("sendStatus");
            String orderCode= request.getParameter("orderCode");
            Page page =orderService.OrderList(Integer.valueOf(pageNo),paymentStatus,sendStatus,orderCode,userId,10);
            mv.addAttribute("page",page);
            List<Orders> ordersList =page.getList();
            mv.addAttribute("paymentStatus", paymentStatus);
            mv.addAttribute("sendStatus", sendStatus);
            mv.addAttribute("orderCode", orderCode);
            mv.addAttribute("userId", userId);
            mv.addAttribute("ordersList", ordersList);
        }catch (Exception e){

            e.printStackTrace();
        }
        return mv;
    }

    /**
     * Created by jifeng
     * 查询作废订单列表
     * */
    @RequestMapping(value = "/cancelOrderList")
    public Model cancelOrderList(HttpServletRequest request,HttpServletResponse response,Model mv){
        try {
            String pageNo = request.getParameter("pageNo");
            if (pageNo==null){
                pageNo ="1";
            }
            String paymentStatus = request.getParameter("paymentStatus");
            String sendStatus = request.getParameter("sendStatus");
            String orderCode= request.getParameter("orderCode");
            Page page = orderService.cancelOrderList(Integer.valueOf(pageNo),paymentStatus,sendStatus,orderCode,10);
            mv.addAttribute("page",page);
            List<Orders> ordersList =page.getList();
            mv.addAttribute("paymentStatus", paymentStatus);
            mv.addAttribute("sendStatus", sendStatus);
            mv.addAttribute("orderCode", orderCode);
            mv.addAttribute("ordersList",ordersList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return mv;
    }

    /**
     * Created by jifeng
     * 收货地址列表
     * */
    @RequestMapping(value = "/orderAddressList")
    public Model orderAddressList(HttpServletRequest request,HttpServletResponse response,Model mv){
        try {
            String pageNo = request.getParameter("pageNo");
            if (pageNo==null){
                pageNo="1";
            }
            String orderCode= request.getParameter("orderCode");
            String paymentStatus = request.getParameter("paymentStatus");
            Page page = orderService.OrderAddressList(Integer.valueOf(pageNo),orderCode,paymentStatus,10);
            mv.addAttribute("page",page);
            List<OrderAddress> orderAddressList =page.getList();
            mv.addAttribute("orderCode", orderCode);
            mv.addAttribute("paymentStatus", paymentStatus);
            mv.addAttribute("orderAddressList",orderAddressList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return mv;
    }

    /**
     * Created by jifeng
     * 删除订单
     * */
    @RequestMapping(value = "/deleteOrder")
    public String deleteOrder(HttpServletRequest request,HttpServletResponse response){
        String ids= request.getParameter("ids");
        if (ids!=null&&ids!="") {
            orderService.deleteOrder(ids);
        }
        return "redirect:/cancelOrderList";
    }


    /**
     * Created by jifeng
     * 查看订单
     * */
    @RequestMapping("/orderDetails")
    public Model orderDetails(HttpServletRequest request,HttpServletResponse response,Model mv){
        String id = request.getParameter("id");
        OrderAddress orderAddress=orderService.orderDetails(Integer.valueOf(id));
        mv.addAttribute("orderAddress",orderAddress);
        return mv;
    }

    /**
     * Created by jifeng
     * 跳到编辑订单
     * */
    @RequestMapping(value = "/orderEdit")
    public Model orderEdit(HttpServletRequest request,HttpServletResponse response,Model mv){
        String id = request.getParameter("ids1");
        OrderAddress orderAddress=orderService.orderDetails(Integer.valueOf(id));
        mv.addAttribute("orderAddress",orderAddress);
        return mv;
    }

    /**
     * Created by jifeng
     * 编辑订单
     * */
    @RequestMapping("/updateOrder")
    public String updateOrder(HttpServletRequest request,HttpServletResponse response){
        String id = request.getParameter("id");
        String orderCode= request.getParameter("orderCode");
        /*String startDate=request.getParameter("startDate");*/
        String paymentStatus =request.getParameter("paymentStatus");
        /*String paymentTime= request.getParameter("paymentTime");*/
        String toExamineStatus=request.getParameter("toExamineStatus");
        String sendStatus=request.getParameter("sendStatus");
        String orderType= request.getParameter("orderType");
        String paymentType=request.getParameter("paymentType");
        String invoice= request.getParameter("invoice");
        String totalMoney= request.getParameter("totalMoney");
        String detailed= request.getParameter("detailed");
        Orders orders = new Orders();
        orders.setId(Integer.valueOf(id));
        orders.setOrderCode(orderCode);
        orders.setPaymentStatus(paymentStatus);
        orders.setToExamineStatus(toExamineStatus);
        orders.setSendStatus(sendStatus);
        orders.setOrderType(orderType);
        orders.setPaymentType(paymentType);
        orders.setInvoice(Integer.valueOf(invoice));
        orders.setTotalMoney(Long.valueOf(totalMoney));
        orderService.updateOrder(orders);
        Address address = orderService.findAddressById(Integer.valueOf(id));
        orderService.updateAddress(address,detailed);
        return "redirect:/orderList";
    }


    /**
     * Created by jifeng
     * 恢复删除菜单把delete属性改为0
     * */
    @RequestMapping(value = "/recoveryOrder")
    public String recoveryOrder(HttpServletRequest request,HttpServletResponse response){
        String ids = request.getParameter("ids");
        if (ids!=null&&ids!="") {
            orderService.recoveryOrder(ids);
        }
        return "redirect:/orderList";
    }

}
